﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FalconSystemCatalogWebsite.Pages
{
    public partial class Categories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 
                string serial = System.Configuration.ConfigurationManager.AppSettings["Serial"];
                BuildStockCategoriesTree(serial);
            }
        }

        private void BuildStockCategoriesTree(string serial)
        {
            using (var Provider = new FalconSystemModel.FalconSystemProvider())
            { 
                int companyID = Provider.Companies.Where(w => w.GUID == serial).FirstOrDefault().CompanyID;

                var categoriesByCompanyQuery = from cmy in Provider.StockCategoriesByCompanies
                                               join cat in Provider.StockCategories
                                               on cmy.StockCategoryID equals cat.StockCategoryID
                                               where cmy.CompanyID == companyID
                                               select new
                                               {
                                                   cat.StockCategoryID,
                                                   cat.Name,
                                                   cat.ImageUrl,
                                                   cat.ParentStockCategoryID
                                               };

                CategoriesRadTreeView.DataTextField = "Name";
                CategoriesRadTreeView.DataValueField = "ImageUrl";
                CategoriesRadTreeView.DataFieldID = "StockCategoryID";
                CategoriesRadTreeView.DataFieldParentID = "ParentStockCategoryID";

                CategoriesRadTreeView.DataSource = categoriesByCompanyQuery.ToList();
                CategoriesRadTreeView.DataBind();

            }            
        }
    }
}